
SelectMode = {
	PickObjs = {},
	CreateUnit = function ()
		SelectMode.CurFuncSeq = SelectMode.FuncSeq.Pass1
		TRACE("SelectMode - SelectMode Created.\n")
	end,
	HasObj = function ()
		local num = table.getn(SelectMode.PickObjs)
		if num>0 then return true end
		return false
	end,
	AddObj = function (Obj)
		if SelectMode.PickObjs[Obj.__ptr] then
			return
		end
		table.insert(SelectMode.PickObjs,Obj)
		SelectMode.PickObjs[Obj.__ptr] = true
		Obj:SetShowBounding(true)
	end,
	ClearObj = function ()
		SelectMode.ShowBounding(false)
		SelectMode.PickObjs = {}
	end,
	ShowBounding = function (bShow)
		for _,o in ipairs(SelectMode.PickObjs) do
			o:SetShowBounding(bShow)
		end
	end,
	GetCenterPos = function ()
		local Pos = {x=0,y=0,z=0}
		local num = table.getn(SelectMode.PickObjs)
		if num<=0 then return Pos end
		for _,o in ipairs(SelectMode.PickObjs) do
			local CurPos = o:GetPos()
			Vector.Add(Pos,CurPos)
		end
		Vector.Coef(Pos,1/num)
		return Pos
	end,
	FuncSeq = {
		Pass1 = function ()
			if cWindow:MouseButtonDown(DIK.LBUTTON) then
				local Ray = cWindow:GetPickRay()
				local CurObj = g_MgrObj:IntersectRay(Ray)
				if CurObj then
					if not cWindow:KeyDown(DIK.LCONTROL) then
						SelectMode.ClearObj()
					end
					SelectMode.AddObj(CurObj)
				end
			end
		end,
		End = function ()
		end
	}
}

function CheckRect2D(s, e)
	if e.x<s.x then
		e.x, s.x = s.x, e.x
	end
	if e.y<s.y then
		e.y, s.y = s.y, e.y
	end
end

MarqueeMode = {
	CreateUnit = function ()
		--marquee
		local Marquee = cDXUIBase:New()
		Marquee:Lock()
		Marquee:SetPos(0,0)
		Marquee:SetSize(0,0)
		MarqueeMode.CurFuncSeq = MarqueeMode.FuncSeq.Pass1
		MarqueeMode.MouseStart = {x=0,y=0}
		MarqueeMode.MouseEnd = {x=0,y=0}
		MarqueeMode.MouseCoordStart = {x=0,y=0}
		MarqueeMode.MouseCoordEnd = {x=0,y=0}
		MarqueeMode.Marquee = Marquee
		cWindow:AddDXUI(Marquee)
		MarqueeMode.Stop = true
	end,
	Reset = function ()
		MarqueeMode.MouseStart = {x=0,y=0}
		MarqueeMode.MouseEnd = {x=0,y=0}
		MarqueeMode.MouseCoordStart = {x=0,y=0}
		MarqueeMode.MouseCoordEnd = {x=0,y=0}
		MarqueeMode.Marquee:SetPos(0,0)
		MarqueeMode.Marquee:SetSize(0,0)
		MarqueeMode.Stop = true
	end,
	FuncSeq = {
		Pass1 = function ()
			if cWindow:MouseButtonDown(DIK.LBUTTON) then
				local p1 = cWindow:MouseXY()
				local p2 = cWindow:MouseCoord()
				MarqueeMode.MouseStart = p1
				MarqueeMode.MouseCoordStart = p2
				MarqueeMode.MouseEnd = p1
				MarqueeMode.MouseCoordEnd = p2
				TRACE(string.format("MarqueeMode - MouseStart( %d, %d)\n",p1.x,p1.y))
				TRACE(string.format("MarqueeMode - MouseCoordStart( %.2f, %.2f)\n",p2.x,p2.y))
				MarqueeMode.CurFuncSeq = MarqueeMode.FuncSeq.Pass2
				MarqueeMode.Stop = false
			end
		end,
		Pass2 = function ()
			local p1 = cWindow:MouseXY()
			local p2 = cWindow:MouseCoord()
			MarqueeMode.MouseEnd = p1
			MarqueeMode.MouseCoordEnd = p2
			local mStart = MarqueeMode.MouseStart
			local p1Start = {x=mStart.x, y=mStart.y}

			CheckRect2D(p1Start,p1)
			--TRACE(string.format("Marquee (%d,%d)->(%d,%d)\n",p1Start.x,p1Start.y,p1.x,p1.y))
			MarqueeMode.Marquee:SetPos(p1Start.x, p1Start.y)
			MarqueeMode.Marquee:SetSize(p1.x-p1Start.x, p1.y-p1Start.y)
			if not cWindow:MouseButtonDown(DIK.LBUTTON) then
				TRACE(string.format("MarqueeMode - MouseEnd( %d, %d)\n",p1.x,p1.y))
				TRACE(string.format("MarqueeMode - MouseCoordEnd( %.2f, %.2f)\n",p2.x,p2.y))
				MarqueeMode.CurFuncSeq = MarqueeMode.FuncSeq.End
			end
		end,
		End = function ()
			MarqueeMode.CurFuncSeq = MarqueeMode.FuncSeq.Pass1
			MarqueeMode.Reset()
		end
	}
}
